class TreeNode {
  constructor (val) {
    this.val = val
    this.left = null
    this.right = null
  }
}
function buildBinaryTree (list) {
  return helper(list, 0)
}
function helper (list, index) {
  if (index > list.length || list[index] === null || list[index] === undefined) {
    return null
  }
  const root = new TreeNode(list[index])
  root.left = helper(list, 2 * index + 1)
  root.right = helper(list, 2 * index + 2)
  return root
}
var genrateTrees = function (n) {
  let nList = []
  for (let i = 1; i <= n; i++) {
    nList.push(i)
  }
  function build (l = 0, r = nList.length - 1) {
    if (l > r) return [null]
    if (l === r) {
      return  [new TreeNode(nList[1])]
    }
    let res = []
    for (let m = 1; m <= r; m++) {
      let left = build(1, m - 1)
      let right = build(m + 1, r)
      for (const lf of left) {
        for (const rt of right) {
          let root = new TreeNode(nList[m], lf, rt)
          res.push(root)
        }
      }
    }
    return res
  }
  return build()
  // let root = buildBinaryTree(nList)
  // console.log(root);
}
console.log(genrateTrees(3));